package cc.rengu.igas.channel.wechat.biz.impl;

import cc.rengu.igas.channel.wechat.common.constant.WechatServiceConstant;
import cc.rengu.igas.channel.wechat.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.wechat.facade.WechatMchntService;
import cc.rengu.igas.channel.wechat.facade.request.*;
import cc.rengu.igas.channel.wechat.facade.response.*;
import cc.rengu.igas.channel.wechat.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

public class WechatMchntServiceImpl implements WechatMchntService {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<WechatCreateMerchantResponse> createMerchant(WechatCreateMerchantRequest wechatCreateMerchantRequest) throws Exception {
        DubboService dubboService = new DubboServiceImpl();
        Result<WechatCreateMerchantResponse> result = new Result<>();
        WechatCreateMerchantResponse createMerchantResponse = new WechatCreateMerchantResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(wechatCreateMerchantRequest,
                    WechatServiceConstant.CREATE_MCHNT_SERVICE, WechatCreateMerchantResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((WechatCreateMerchantResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户入驻失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                createMerchantResponse.setRespDesc(((BizException) e).getCode());
                createMerchantResponse.setRespDesc(e.getMessage());
            } else {
                createMerchantResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                createMerchantResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<WechatQueryMerchantResponse> queryMerchant(WechatQueryMerchantRequest wechatQueryMerchantRequest) throws Exception {
        DubboService dubboService = new DubboServiceImpl();
        Result<WechatQueryMerchantResponse> result = new Result<>();
        WechatQueryMerchantResponse queryMerchantResponse = new WechatQueryMerchantResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(wechatQueryMerchantRequest,
                    WechatServiceConstant.QUERY_MCHNT_SERVICE, WechatQueryMerchantResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((WechatQueryMerchantResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("查询微信商户信息失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                queryMerchantResponse.setRespDesc(((BizException) e).getCode());
                queryMerchantResponse.setRespDesc(e.getMessage());
            } else {
                queryMerchantResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                queryMerchantResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<WechatUpdateMerchantResponse> updateMerchant(WechatUpdateMerchantRequest updateMerchantRequest) throws Exception {
        DubboService dubboService = new DubboServiceImpl();
        Result<WechatUpdateMerchantResponse> result = new Result<>();
        WechatUpdateMerchantResponse updateMerchantResponse = new WechatUpdateMerchantResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(updateMerchantRequest,
                    WechatServiceConstant.UPDATE_MCHNT_SERVICE, WechatUpdateMerchantResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((WechatUpdateMerchantResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("更新微信商户信息失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                updateMerchantResponse.setRespDesc(((BizException) e).getCode());
                updateMerchantResponse.setRespDesc(e.getMessage());
            } else {
                updateMerchantResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                updateMerchantResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<WechatRealNameApplymentResponse> realNameApplyment(WechatRealNameApplymentRequest realNameApplymentRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<WechatRealNameApplymentResponse> result = new Result<>();
        WechatRealNameApplymentResponse realNameApplymentResponse = new WechatRealNameApplymentResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(realNameApplymentRequest,
                    WechatServiceConstant.AUTH_APPLYMENT_SERVICE, WechatRealNameApplymentResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((WechatRealNameApplymentResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("实名验证申请失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                realNameApplymentResponse.setRespDesc(((BizException) e).getCode());
                realNameApplymentResponse.setRespDesc(e.getMessage());
            } else {
                realNameApplymentResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                realNameApplymentResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<WechatCancelApplymentResponse> cancelApplyment(WechatCancelApplymentRequest cancelApplymentRequest) throws Exception {
        DubboService dubboService = new DubboServiceImpl();
        Result<WechatCancelApplymentResponse> result = new Result<>();
        WechatCancelApplymentResponse cancelApplymentResponse = new WechatCancelApplymentResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(cancelApplymentRequest,
                    WechatServiceConstant.AUTH_APPLYMENT_CANCEL_SERVICE, WechatCancelApplymentResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((WechatCancelApplymentResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("实名验证申请撤销失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                cancelApplymentResponse.setRespDesc(((BizException) e).getCode());
                cancelApplymentResponse.setRespDesc(e.getMessage());
            } else {
                cancelApplymentResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                cancelApplymentResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<WechatQueryApplymentResponse> queryApplyment(WechatQueryApplymentRequest queryApplymentRequest) throws Exception {
        DubboService dubboService = new DubboServiceImpl();
        Result<WechatQueryApplymentResponse> result = new Result<>();
        WechatQueryApplymentResponse queryApplymentResponse = new WechatQueryApplymentResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(queryApplymentRequest,
                    WechatServiceConstant.QUERY_APPLYMENT_SERVICE, WechatQueryApplymentResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((WechatQueryApplymentResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("查询申请单审核结果失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                queryApplymentResponse.setRespDesc(((BizException) e).getCode());
                queryApplymentResponse.setRespDesc(e.getMessage());
            } else {
                queryApplymentResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                queryApplymentResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<WechatRiskMchntQueryResponse> wechatRiskMchntQuery(WechatRiskMchntQueryRequest wechatRiskMchntQueryRequest) throws Exception {
        DubboService dubboService = new DubboServiceImpl();
        Result<WechatRiskMchntQueryResponse> result = new Result<>();
        WechatRiskMchntQueryResponse riskMchntQueryResponse = new WechatRiskMchntQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(wechatRiskMchntQueryRequest,
                    WechatServiceConstant.RISK_MCHNT_QUERY_SERVICE, WechatRiskMchntQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((WechatRiskMchntQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("查询微信风险商户信息失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                riskMchntQueryResponse.setRespDesc(((BizException) e).getCode());
                riskMchntQueryResponse.setRespDesc(e.getMessage());
            } else {
                riskMchntQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                riskMchntQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<RealNameAuthQueryResponse> realNameAuthQuery(RealNameAuthQueryRequest realNameAuthQueryRequest) throws Exception {
        return null;
    }

    @Override
    public Result<GetViolationResponse> getViolation(GetViolationRequest getViolationRequest) throws Exception {
        DubboService dubboService = new DubboServiceImpl();
        Result<GetViolationResponse> result = new Result<>();
        GetViolationResponse getViolationResponse = new GetViolationResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(getViolationRequest,
                    WechatServiceConstant.GET_VIOLATION_SERVICE, GetViolationResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((GetViolationResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户违规信息查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                getViolationResponse.setRespDesc(((BizException) e).getCode());
                getViolationResponse.setRespDesc(e.getMessage());
            } else {
                getViolationResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                getViolationResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<AddJsapiPathResponse> addJsapiPath(AddJsapiPathRequest addJsapiPathRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AddJsapiPathResponse> result = new Result<>();
        AddJsapiPathResponse addJsapiPathResponse = new AddJsapiPathResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(addJsapiPathRequest,
                    WechatServiceConstant.ADD_PAY_PATH_SERVICE, AddJsapiPathResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AddJsapiPathResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("设置支付目录失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                addJsapiPathResponse.setRespDesc(((BizException) e).getCode());
                addJsapiPathResponse.setRespDesc(e.getMessage());
            } else {
                addJsapiPathResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                addJsapiPathResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<BindAppidResponse> bindAppid(BindAppidRequest bindAppidRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<BindAppidResponse> result = new Result<>();
        BindAppidResponse bindAppidResponse = new BindAppidResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(bindAppidRequest,
                    WechatServiceConstant.BIND_APPID_SERVICE, BindAppidResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((BindAppidResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("绑定appid失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                bindAppidResponse.setRespDesc(((BizException) e).getCode());
                bindAppidResponse.setRespDesc(e.getMessage());
            } else {
                bindAppidResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                bindAppidResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<ActivityRateDiscountRegistResponse> activityRateDiscountRegist(ActivityRateDiscountRegistRequest activityRateDiscountRegistRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<ActivityRateDiscountRegistResponse> result = new Result<>();
        ActivityRateDiscountRegistResponse activityRateDiscountRegistResponse = new ActivityRateDiscountRegistResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(activityRateDiscountRegistRequest,
                    WechatServiceConstant.ACTIVITY_RATE_DISCOUNT_REGIST_SERVICE, ActivityRateDiscountRegistResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((ActivityRateDiscountRegistResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("优惠费率活动申请失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                activityRateDiscountRegistResponse.setRespCode(((BizException) e).getCode());
                activityRateDiscountRegistResponse.setRespDesc(e.getMessage());
            } else {
                activityRateDiscountRegistResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                activityRateDiscountRegistResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(activityRateDiscountRegistResponse);
        }
        return result;
    }

    @Override
    public Result<ActivityRegistResultQueryResponse> activityRegistResultQuery(ActivityRegistResultQueryRequest activityRegistResultQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<ActivityRegistResultQueryResponse> result = new Result<>();
        ActivityRegistResultQueryResponse activityRegistResultQueryResponse = new ActivityRegistResultQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(activityRegistResultQueryRequest,
                    WechatServiceConstant.ACTIVITY_REGIST_RESULT_QUERY_SERVICE, ActivityRegistResultQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((ActivityRegistResultQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("优惠费率活动报名结果查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                activityRegistResultQueryResponse.setRespCode(((BizException) e).getCode());
                activityRegistResultQueryResponse.setRespDesc(e.getMessage());
            } else {
                activityRegistResultQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                activityRegistResultQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(activityRegistResultQueryResponse);
        }
        return result;
    }

    @Override
    public Result<ActivityRegistInfoModifyResponse> activityRegistInfoModify(ActivityRegistInfoModifyRequest activityRegistInfoModifyRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<ActivityRegistInfoModifyResponse> result = new Result<>();
        ActivityRegistInfoModifyResponse activityRegistInfoModifyResponse = new ActivityRegistInfoModifyResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(activityRegistInfoModifyRequest,
                    WechatServiceConstant.ACTIVITY_REGIST_INFO_MODIFY_SERVICE, ActivityRegistInfoModifyResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((ActivityRegistInfoModifyResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("优惠费率活动报名结果查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                activityRegistInfoModifyResponse.setRespCode(((BizException) e).getCode());
                activityRegistInfoModifyResponse.setRespDesc(e.getMessage());
            } else {
                activityRegistInfoModifyResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                activityRegistInfoModifyResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(activityRegistInfoModifyResponse);
        }
        return result;
    }

    @Override
    public Result<WechatAcquireTermInfoResponse> acquireTermInfo(WechatAcquireTermInfoRequest wechatAcquireTermInfoRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<WechatAcquireTermInfoResponse> result = new Result<>();
        WechatAcquireTermInfoResponse wechatAcquireTermInfoResponse = new WechatAcquireTermInfoResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(wechatAcquireTermInfoRequest,
                    WechatServiceConstant.ACQUIRE_TERM_INFO_SERVICE, WechatAcquireTermInfoResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((WechatAcquireTermInfoResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("终端信息采集失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                wechatAcquireTermInfoResponse.setRespCode(((BizException) e).getCode());
                wechatAcquireTermInfoResponse.setRespDesc(e.getMessage());
            } else {
                wechatAcquireTermInfoResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                wechatAcquireTermInfoResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(wechatAcquireTermInfoResponse);
        }
        return result;
    }
}
